#include "Cuboid.h"

#include "Face.h"

Cuboid::Cuboid():
		m_dimensions(1.0f, 1.0f, 1.0f)
{
	init();
}

Cuboid::Cuboid(const Vertex& dim):
		m_dimensions(dim)
{
	init();
}

Cuboid::Cuboid(float w, float h, float t):
		m_dimensions(w, h, t)
{
	init();
}

Cuboid::Cuboid(const Vertex& dim, const Vertex& pos):
		Assembly(pos), m_dimensions(dim)
{
	init();
}

Cuboid::Cuboid(float w, float h, float t, float x, float y, float z):
		Assembly(x, y, z), m_dimensions(w, h, t)
{
	init();
}

Cuboid::Cuboid(const Vertex& dim, const Vertex& pos, const Vertex& rot):
		Assembly(pos, rot), m_dimensions(dim)
{
	init();
}

Cuboid::Cuboid(float w, float h, float t,
               float x, float y, float z,
               float a, float b, float g):
		Assembly(x, y, z, a, b, g), m_dimensions(w, h, t)
{
	init();
}

Cuboid::~Cuboid()
{
	for (vector<Vertex*>::iterator it = m_vertex.begin(); it != m_vertex.end(); it++)
	{
		delete *it;
	}
	
	for (vector<Vertex*>::iterator it = m_normal.begin(); it != m_normal.end(); it++)
	{
		delete *it;
	}
}

const Vertex& Cuboid::dimensions() const
{
	return m_dimensions;
}

Vertex& Cuboid::dimensions()
{
	m_v_mod = true;
	return m_dimensions;
}

void Cuboid::init()
{
	m_name = "cuboid";
	m_vertex.resize(8);
	m_vertex[0] = new Vertex;
	m_vertex[1] = new Vertex;
	m_vertex[2] = new Vertex;
	m_vertex[3] = new Vertex;
	m_vertex[4] = new Vertex;
	m_vertex[5] = new Vertex;
	m_vertex[6] = new Vertex;
	m_vertex[7] = new Vertex;
	
	m_normal.resize(6);
	m_normal[0] = new Vertex(0.0f, 0.0f, 1.0f, 0.0f);
	m_normal[1] = new Vertex(1.0f, 0.0f, 0.0f, 0.0f);
	m_normal[2] = new Vertex(0.0f, 0.0f, -1.0f, 0.0f);
	m_normal[3] = new Vertex(-1.0f, 0.0f, 0.0f, 0.0f);
	m_normal[4] = new Vertex(0.0f, 1.0f, 0.0f, 0.0f);
	m_normal[5] = new Vertex(0.0f, -1.0f, 0.0f, 0.0f);
	
	// front
	Face* face = new Face(false);
	face->push_back(m_vertex[0], m_normal[0]);
	face->push_back(m_vertex[1], m_normal[0]);
	face->push_back(m_vertex[2], m_normal[0]);
	face->push_back(m_vertex[3], m_normal[0]);
	m_face.push_back(face);
	
	// right
	face = new Face(false);
	face->push_back(m_vertex[3], m_normal[1]);
	face->push_back(m_vertex[2], m_normal[1]);
	face->push_back(m_vertex[4], m_normal[1]);
	face->push_back(m_vertex[5], m_normal[1]);
	m_face.push_back(face);
	
	// back
	face = new Face(false);
	face->push_back(m_vertex[5], m_normal[2]);
	face->push_back(m_vertex[4], m_normal[2]);
	face->push_back(m_vertex[6], m_normal[2]);
	face->push_back(m_vertex[7], m_normal[2]);
	m_face.push_back(face);
	
	// left
	face = new Face(false);
	face->push_back(m_vertex[7], m_normal[3]);
	face->push_back(m_vertex[6], m_normal[3]);
	face->push_back(m_vertex[1], m_normal[3]);
	face->push_back(m_vertex[0], m_normal[3]);
	m_face.push_back(face);
	
	// top
	face = new Face(false);
	face->push_back(m_vertex[7], m_normal[4]);
	face->push_back(m_vertex[0], m_normal[4]);
	face->push_back(m_vertex[3], m_normal[4]);
	face->push_back(m_vertex[5], m_normal[4]);
	m_face.push_back(face);
	
	// bottom
	face = new Face(false);
	face->push_back(m_vertex[1], m_normal[5]);
	face->push_back(m_vertex[6], m_normal[5]);
	face->push_back(m_vertex[4], m_normal[5]);
	face->push_back(m_vertex[2], m_normal[5]);
	m_face.push_back(face);
}

void Cuboid::update_vertex()
{
	float x, y, z;
	m_dimensions.getAll(x, y, z);
	x /= 2.0f;
	y /= 2.0f;
	z /= 2.0f;
	m_vertex[0]->setAll(-x, y, z);
	m_vertex[1]->setAll(-x, -y, z);
	m_vertex[2]->setAll(x, -y, z);
	m_vertex[3]->setAll(x, y, z);
	m_vertex[4]->setAll(x, -y, -z);
	m_vertex[5]->setAll(x, y, -z);
	m_vertex[6]->setAll(-x, -y, -z);
	m_vertex[7]->setAll(-x, y, -z);
	
	Assembly::update_vertex();
}

